package com.z.service.impl;

import com.z.dao.GoodsDao;
import com.z.dao.impl.GoodsDaoImpl;
import com.z.pojo.Goods;
import com.z.pojo.PageBean;
import com.z.service.GoodsService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张福兴
 * date 2021/9/4
 */
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public Goods goodsInfo(Integer gid) {
        Goods goods = null;
        try {
            goods = goodsDao.selectOneByGid(gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goods;
    }

    @Override
    public PageBean<Goods> goodsFormType(Integer nowpage, Integer eachcount, String term) {
        List<Goods> list = null;
        Integer count = null;
        try {
            list = goodsDao.selectGoodsByTid(nowpage, eachcount, term);
            count = goodsDao.count(term);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageBean<Goods> pageBean = new PageBean<>();

        pageBean.setNowpage(nowpage);
        pageBean.setEachcount(eachcount);
        pageBean.setLists(list);
        pageBean.setTotalcount(count);
        return pageBean;
    }

    @Override
    public Map<String, List<Goods>> IndexGoods() {
        Map<String, List < Goods >> map = new HashMap<>();
        List<Goods> list1 = null;
        List<Goods> list2 = null;
        List<Goods> list3 = null;
        List<Goods> list4 = null;
        try {
            list1 = goodsDao.selectGoodsByTid(1, 4, " tid=1");
            list2 = goodsDao.selectGoodsByTid(1, 4, " tid=2");
            list3 = goodsDao.selectGoodsByTid(1, 4, " tid=3");
            list4 = goodsDao.selectGoodsByTid(1, 4, " tid=4");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        map.put("list1",list1);
        map.put("list2",list2);
        map.put("list3",list3);
        map.put("list4",list4);

        return map;
    }

    @Override
    public Integer addGoods(Goods goods) {
        Integer flag = 0;
        try {
            flag = goodsDao.insertGoods(goods);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public Integer deleteGoods(Integer gid) {
        Integer row = null;
        try {
            row = goodsDao.deleteGoods(gid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return row;
    }

    @Override
    public Integer updataGoods(Goods goods) throws SQLException {
        return goodsDao.updataGoods(goods);
    }
}
